﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BranchOfSansar.Sansar.Register;
using BranchOfSansar.Sansar.Enquiry;
using System.ServiceModel;
using DevExpress.XtraEditors;
namespace BranchOfSansar
{
    public partial class frmApartment : Form
    {
        private Apartment apt;
        public frmApartment()
        {
            InitializeComponent();
        }
        private void loadData()
        {
            
            using (EnquiryClient ct = new EnquiryClient(Globals.Token))
            {
                cmbDistrict.Properties.Items.AddRange(ct.DistrictList().Select(s => new ComboItem() { Display = s.Name, StrValue = s.DistrictCode }).ToArray());                               
                cmbSection.Properties.Items.AddRange(ct.SectionList().Select(s1 => new ComboItem() { Display = s1.Name, StrValue = s1.SectionNo.ToString() }).ToArray());
                cmbBank.Properties.Items.AddRange(ct.BankList().Select(s => new ComboItem() { Display = s.Name, StrValue = s.BankID.ToString() }).ToArray());
                cmbKontor.Properties.Items.AddRange(ct.KontorList().Select(s => new ComboItem() { Display = s.Name, StrValue = s.KontorNo.ToString() }).ToArray());

                cmbDistrict.SelectedIndex = cmbDistrict.Properties.Items.Count > 0 ? 0 : -1;
                cmbSection.SelectedIndex = cmbSection.Properties.Items.Count > 0 ? 0 : -1;                                            
                cmbBank.SelectedIndex = cmbBank.Properties.Items.Count > 0 ? 0 : -1;
                cmbKontor.SelectedIndex = cmbKontor.Properties.Items.Count > 0 ? 0 : -1;
            }            
            
        }
        private void loadEdit()
        {
            if (this.Tag.ToString().Equals("new"))
            {
                apt = new Apartment();
            }
            else
            {
                using (RegisterClient obj = new RegisterClient(Globals.Token))
                {
                    apt = obj.GetApartment(Convert.ToInt32(this.Tag));
                    txtApartCode.Text = apt.ApartCode;
                    txtDugaar1.Text = apt.ApartNo1;
                    txtDugaar2.Text = apt.ApartNo2;
                    txtName.Text = apt.Description;
                    txtOldCode.Text = apt.ApartOldCode;
                    txtParent.Text = apt.ParentLinkCode;
                    Apartment ap = obj.GetApartmentByCode(txtParent.Text);
                    if (ap != null)
                    {
                        txtParentName.Text = obj.GetApartmentByCode(txtParent.Text).Description;
                    }                                            
                    txtPartNum.Text = apt.PartNo.ToString();
                    txtZipCode.Text = apt.ZipCode;
                    txtAddr.Text = apt.PositionName;
                    Globals.setComboSelectedItem(cmbSection, apt.SectionNo.ToString());
                    Globals.setComboSelectedItem(cmbKontor, apt.KontorNo.ToString());

                    Globals.setComboSelectedItem(cmbDistrict, obj.GetQuarter(apt.QuarterCode).DistrictCode);
                    Globals.setComboSelectedItem(cmbQuarter, apt.QuarterCode);
                }
                using (RegisterClient obj = new RegisterClient(Globals.Token))
                {
                    Globals.setComboSelectedItem(cmbBank, obj.GetKass(apt.BankBranchNo).BankID.ToString());
                    Globals.setComboSelectedItem(cmbKass, apt.BankBranchNo.ToString());
                }                
            }
        }
        
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            saveApartment();
        }
        
        private void saveApartment()
        {
            if (!Globals.DoValidation(panelControl2))
            {
                return;
            }
            apt.ApartCode=txtApartCode.Text;
            apt.ApartNo1 = txtDugaar1.Text;
            apt.ApartNo2 = txtDugaar2.Text;
            apt.Description = txtName.Text;
            apt.ApartOldCode = txtOldCode.Text;
            apt.ParentLinkCode = txtParent.Text;
            apt.PartNo = txtPartNum.Text;
            apt.ZipCode = txtZipCode.Text;
            apt.PositionName = txtAddr.Text;
            apt.SectionNo = Convert.ToInt32(((ComboItem)cmbSection.SelectedItem).StrValue);
            apt.KontorNo = Convert.ToInt32(((ComboItem)cmbKontor.SelectedItem).StrValue);
            apt.BankBranchNo = Convert.ToInt32(((ComboItem)cmbKass.SelectedItem).StrValue);
            apt.QuarterCode = ((ComboItem)cmbQuarter.SelectedItem).StrValue;
            using (RegisterClient obj = new RegisterClient(Globals.Token))
            {
                Result r;
                if (this.Tag == "new")
                {
                    r=obj.AddApartment(apt);
                }
                else
                {
                    r=obj.EditApartment(apt);
                }
                if (r.Code != 0)
                {
                    Globals.Err(r.Message);
                    return;
                }
                Globals.Info(r.Message);
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            
        }

        private void frmApartment_Load(object sender, EventArgs e)
        {
            loadData();
            loadEdit();
        }

        private void cmbBank_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (EnquiryClient obj = new EnquiryClient(Globals.Token))
            {
                cmbKass.Properties.Items.Clear();
                cmbKass.Properties.Items.AddRange(obj.BankBranches(Convert.ToInt32(((ComboItem)cmbBank.SelectedItem).StrValue)).Select(s => new ComboItem() { Display = s.Name, StrValue = s.BranchID.ToString() }).ToArray());
                cmbKass.SelectedIndex = cmbKass.Properties.Items.Count > 0 ? 0 : -1;
            }
        }

        private void cmbDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (EnquiryClient obj = new EnquiryClient(Globals.Token))
            {
                cmbQuarter.Properties.Items.Clear();
                cmbQuarter.Properties.Items.AddRange(obj.QuarterList(((ComboItem)cmbDistrict.SelectedItem).StrValue).Select(s => new ComboItem() { Display = s.Name, StrValue = s.QuarterCode }).ToArray());
                cmbQuarter.SelectedIndex = cmbQuarter.Properties.Items.Count > 0 ? 0 : -1;
            }
        }

        private void txtParent_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar== (char)Keys.Enter)
            {
                using (RegisterClient obj = new RegisterClient(Globals.Token))
                {
                    Apartment ap = obj.GetApartmentByCode(txtParent.Text);
                    if (ap == null)
                    {
                        Globals.Info(txtParent.Text + " кодтой байр байхгүй байна!");
                    }
                    else
                    {
                        txtParentName.Text = obj.GetApartmentByCode(txtParent.Text).Description;
                    }
                }
            }
        }
        

       private void cmdExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

       private void txtParent_EditValueChanged(object sender, EventArgs e)
       {

       }
    }
}
